/**
 * @file   ReceivedPUTFILEAuthFile.java
 * @date   2012-12-4下午10:28:26
 * @author liqing
 */
package com.gzceb.zhgjj.mina.PUTFILE;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gzceb.zhgjj.mina.MinaConstants;
import com.gzceb.zhgjj.model.Constants;
import com.gzceb.zhgjj.model.PUTFILEAuthFile;
import com.gzceb.zhgjj.util.PropertiesUtils;



/**
 * <p>PUTFILE接口授权文件收发类。</p>
 *
 * @date   2012-12-4下午10:28:26 
 * @author liqing
 * @description  主要用于错误文件的授权文件接收
 */
public class ReceivedPUTFILEAuthFile extends IoHandlerAdapter {

	private  final Logger logger = LoggerFactory.getLogger(ReceivedPUTFILEAuthFile.class);
	private PUTFILEAuthFile pfAuthFile;
	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandlerAdapter#sessionCreated(org.apache.mina.core.session.IoSession)
	 */
	@Override
	public void sessionCreated(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionCreated(session);
		pfAuthFile = new PUTFILEAuthFile(PropertiesUtils.getValue("destPath_PFAuth"));
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandlerAdapter#sessionOpened(org.apache.mina.core.session.IoSession)
	 */
	@Override
	public void sessionOpened(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionOpened(session);
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandlerAdapter#sessionClosed(org.apache.mina.core.session.IoSession)
	 */
	@Override
	public void sessionClosed(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionClosed(session);
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandlerAdapter#sessionIdle(org.apache.mina.core.session.IoSession, org.apache.mina.core.session.IdleStatus)
	 */
	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		logger.debug("关闭闲置session，ip：" + session.getRemoteAddress().toString());
    	session.close(false);
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandlerAdapter#exceptionCaught(org.apache.mina.core.session.IoSession, java.lang.Throwable)
	 */
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		
		logger.debug("关闭异常 session，ip：" + session.getRemoteAddress().toString());
    	session.close(true);
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandlerAdapter#messageReceived(org.apache.mina.core.session.IoSession, java.lang.Object)
	 */
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		// 
		String rspStr = pfAuthFile.ProcRcvFile((String)message);
		//如果中心返回的4号结构体显示接收已经完成或者失败，本端都不再做响应返回
        if(! MinaConstants.F_OVER.getValue().equals(rspStr)){
        	session.write(rspStr);
        }
        
	}

	/* (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandlerAdapter#messageSent(org.apache.mina.core.session.IoSession, java.lang.Object)
	 */
	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		// TODO Auto-generated method stub
		super.messageSent(session, message);
	}

}
